<style lang="scss" scoped>
.hospital {
  height: 100%;
  background-color: #f0f0f2;

  .hospital-list {
    padding-top: 0.44rem;
    .search{
      margin: 0.1rem 0.1rem 0.05rem;
      span{
        font-size: 0.16rem;
      }
        .item-label{
        font-size: 0.16rem;
        width: 1rem;
        padding: 0.1rem 0 0.1rem 0.15rem;
        color: #98A5AB;
        display: inline-block;
      }
      .item-content{
        display: inline-block;
        width: calc(100% - 1.5rem);
      }
    }
  }
}
</style>

<template>
     <div class="hospital">
        <div class="topbar">
            <span class="leftbtn" @click="goback"><Icon type="ios-arrow-back"></Icon></span>
            <span class="centertitle">医院列表</span>
            <span class="rightbtn" @click="addHospital">新增</span>    
        </div>     
        <div class="hospital-list">
          <div class="search">
            <div class="item-label">经销商筛选</div>
            <div class="item-content">
              <Select  v-model="agentId" clearable placeholder="请选择经销商" @on-change="agentChange">
                  <Option v-for="item in agentList" :value="item.id" :key="item.id" >{{ item.agentName }}</Option>
              </Select>
            </div>
          </div>

            <el-table :data="hospitalList" @row-click="dblclick" >
                <el-table-column  label="医院名称" prop="hospitalName"></el-table-column>
                <el-table-column  label="医院地址" prop="hospitalAddr"></el-table-column>
                <el-table-column  label="联系人" prop="hospitalLinker"></el-table-column>
                <el-table-column label="联系人电话" prop="hospitalPhone"></el-table-column>                
            </el-table>
        </div>
    </div>   
</template>

<script>
import {sampleData} from 'api/index.js'
export default {
  data() {
    return {
      agentList:[],
      agentId:'1',
      hospitalList: []
    };
  },
  methods: {
    goback() {
      this.$router.push('/index')
    },
    addHospital(){
      this.$store.state.agentId=this.agentId;
      this.$router.push('/hospital')
      this.$store.state.agentOperation="add";
    },
    dblclick(row){
        this.$store.state.agentOperation="edit";
        this.$router.push({path:'hospital',query:{hospitalId:row.hospitalId}});
    },
    searchAgent(){
      this.$store.state.agentId=this.agentId;
      this.getHospitalListByAgentId();
    },
    agentChange(){
      this.getHospitalListByAgentId()
    },
    getHospitalListByAgentId(){
      let obj={
        agentId:this.agentId
      }
      sampleData.getHospitalListByAgentId(obj).then(res=>{
        if(res.code==0||res.code==200){
          this.hospitalList=res.list;
        }else{
          this.$Message.error(res.msg)
        }
      }).catch(error=>{
        this.$Message.error(error)
      })
    },
    getAgentListByUserId(){
      let obj={}
      sampleData.getAgentListByUserId(obj).then(res=>{
        if (res.code == 0 || res.code == 200) {
          this.agentList=res.list;
        }       
      }).catch(error=>{
        this.$Message.error(error)
      })
    } 
  },
  mounted() {
    if(this.$store.state.agentId!=""){
      this.agentId=this.$store.state.agentId;
    }
    this.getHospitalListByAgentId();
    this.getAgentListByUserId();
  },
};
</script>


